<template>
  <div class="flexContent">
    <div class="searchBox">
      <el-form ref="formRef" class="demo-form-inline" @submit.native.prevent label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="消息名称">
                  <el-input v-model="msgName" style="width: 95%" placeholder="请输入..." clearable
                    @keyup.enter.native="seachFn" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="seachFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="addFn">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" class="tablebox" border ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          align="center">
          <vxe-column type="seq" title="序号" width="80"></vxe-column>
          <vxe-column field="name" title="消息名称" />
          <vxe-column field="content" title="消息内容" />
          <vxe-column title="状态">
            <template v-slot="{ row }">
              <el-tag v-if="row.status === '0'" disabled type="success" plain>已启用</el-tag>
              <el-tag v-if="row.status === '1'" disabled type="info" plain>已停用</el-tag>
            </template>
          </vxe-column>
          <vxe-column title="操作" fixed="right" width="180">
            <template v-slot="{ row }">
              <el-button type="text" size="big" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="getDetail(row)">详情</el-button>
              <el-button slot="reference" style="color: rgb(0, 132, 255); margin-right: 10px" type="text" size="big"
                @click.stop="edit(row)">修改</el-button>
              <el-button type="text" size="big" style="margin-right: 10px; color: red"
                @click="$delFn('消息', del, row)">删除</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <AddDialog :close-on-click-modal="false" :is-show-btn="isShowBtn" :show-dialog.sync="showDialog" :width="width"
      :title="title" @btnOk="btnOk" @close="close">
      <el-form ref="addFormRef" :disabled="title === '详情'" :model="addForm" :rules="addRules" label-width="80px">
        <el-form-item label="消息名称" style="width: 100%" prop="name">
          <el-input v-model="addForm.name" />
        </el-form-item>
        <el-form-item label="消息内容" style="width: 100%" prop="content">
          <el-input v-model="addForm.content" type="textarea" maxlength="200" :rows="4" show-word-limit />
        </el-form-item>
        <el-form-item label="是否启用" style="width: 100%" prop="status">
          <el-radio-group v-model="addForm.status" size="small">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </AddDialog>
  </div>
</template>

<script>
import AddDialog from "@/components/Goods/AddDialog";
import { getMsgList, addMsg, updateMsg, deleteMsg } from "@/api/h5";
export default {
  name: "MsgInfo",
  components: {
    AddDialog,
  },

  data() {
    return {
      isShowBtn: false,
      width: "40%",
      title: "新增",
      showDialog: false,
      msgName: "",
      isSearch: false,
      addForm: {
        name: "",
        content: "",
        status: "", // 0.启用，1.停用
      },
      addRules: {
        name: [
          {
            required: true,
            message: "请输入消息名称",
            trigger: "blur",
          },
          {
            min: 1,
            max: 20,
            message: "长度在1到20个字符",
            trigger: "blur",
          },
          {
            pattern: /^([\u4e00-\u9fa5])+$/,
            message: "只能输入中文",
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: "请输入消息内容",
            trigger: "blur",
          },
        ],
        deviceBrand: [
          {
            min: 1,
            max: 200,
            message: "长度在1到200个字符",
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "blur",
          },
        ],
      },
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      msgList: [],
      msgId: "",
    };
  },
  created() {
    this.getMsgList();
  },
  methods: {
    async getMsgList() {
      if (!this.isSearch) {
        this.msgName = "";
      }
      const res = await getMsgList({ ...this.page, name: this.msgName });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        console.log(result);
        this.msgList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("msgList", this.msgList);
        const $table = this.$refs.xTable1
        $table.loadData(this.msgList);
      } else {
        // this.$message.error(res.message)
      }
    },
    async seachFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.getMsgList();
    },
    addFn() {
      this.title = "新增";
      this.showDialog = true;
    },
    async del(row) {
      const res = await deleteMsg({ id: row.id });
      if (res.code === 200) {
        this.$message.success("删除成功");
        if (this.msgList.length === 1 && this.page.pageNum > 1)
          this.page.pageNum--;
        this.getMsgList();
      } else {
        // this.$message.error(res.message);
      }
    },
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearching = false;
      this.msgName = "";
      this.getMsgList();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      // console.log(`每页 ${val} 条`
      this.getMsgList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.page.pageNum = val;
      this.getMsgList();
    },
    close() {
      this.showDialog = false;
      this.addForm = {
        name: "",
        content: "",
        status: "",
      };
      this.$refs.addFormRef.resetFields();
    },
    btnOk() {
      // 判断是新增还是修改
      if (this.title === "新增") {
        // 新增
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            //
            const res = await addMsg(this.addForm);
            if (res.code === 200) {
              this.$message.success("新增成功");
              this.getMsgList();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      } else if (this.title === "详情") {
        // 关闭
        this.close();
      } else if (this.title === "修改") {
        // 修改
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            const res = await updateMsg({ ...this.addForm, id: this.msgId });
            if (res.code === 200) {
              this.$message.success("修改成功");
              this.getMsgList();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      }
    },
    getDetail(row) {
      this.showDialog = true;
      this.title = "详情";
      console.log(row);
      this.isShowBtn = true;
      this.addForm.name = row.name;
      this.addForm.content = row.content;
      this.addForm.status = row.status;
    },
    edit(row) {
      this.showDialog = true;
      this.title = "修改";
      console.log(row);
      this.msgId = row.id;
      this.addForm.name = row.name;
      this.addForm.content = row.content;
      this.addForm.status = row.status;
    },
  },
};
</script>

<style lang="scss" scoped></style>
